package com.yicheng.statistics.actor

import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.ZoneId
import java.util.Date

import scala.concurrent.ExecutionContext.Implicits.global

import com.yicheng.statistics.actor.analysis.OverSpeed
import com.yicheng.statistics.repo.RTAModel.BaseAlarm
import com.yicheng.statistics.service.HTAService

import akka.actor.Actor
import akka.actor.ActorLogging
import akka.actor.ActorRef
import akka.actor.Props
import akka.actor.SupervisorStrategy.Stop
import com.yicheng.statistics.repo.model.AlarmUtils
import com.yicheng.statistics.actor.analysis.Mileage
import com.yicheng.statistics.actor.analysis.TriedDrive
import com.yicheng.statistics.actor.analysis.AreaInOut
import com.yicheng.statistics.actor.analysis.BatteryAlarmActor

/**
  * Created by yuer on 2016/12/1.
  * hta 路由类
  */

case class BaseAlarmData(alarmType: Int, baseAlarm: BaseAlarm)

class HtaRouting extends Actor with ActorLogging {
  lazy val timeZone: ZoneId = ZoneId.systemDefault
  val firstTime:Date = Date.from(LocalDateTime.of(LocalDate.now.minusDays(1), LocalTime.MIN).
    atZone(timeZone).toInstant)
  val lastTime:Date = Date.from(LocalDateTime.of(LocalDate.now.minusDays(1), LocalTime.MAX).
    atZone(timeZone).toInstant)
  lazy val currentMileage = 33554443
  
  val overSpeed : ActorRef = context.child("OverSpeed").getOrElse(context.actorOf(Props[OverSpeed], "OverSpeed"))//超速处理actor
  val mileage: ActorRef = context.child("Mileage").getOrElse(context.actorOf(Props[Mileage],"Mileage"))//里程统计
  val triedDrive: ActorRef = context.child("TriedDrive").getOrElse(context.actorOf(Props[TriedDrive],"TriedDrive"))//里程统计
  val areaInOut: ActorRef = context.child("AreaInOut").getOrElse(context.actorOf(Props[AreaInOut],"AreaInOut"))//里程统计
  val batteryAlarm: ActorRef = context.child("BatteryAlarm").getOrElse(context.actorOf(Props[BatteryAlarmActor],"BatteryAlarm"))//电池报警统计
  
  override def receive: Receive = {
    case (deviceId: String, deviceType: Int) => 
      log.info("recive device "+deviceId+ " "+deviceType)
      HTAService.getAnalyseGroupsByDevice(deviceId, deviceType).map { analyseGroup =>
        if(analyseGroup.size > 0){
          analyseGroup foreach (info => {//循环分析组根据分析组的不同发送到
            log.info(info.toString())
            if( info.alarmTypeID == AlarmUtils.vehicle_speed){//如果有超速报警设置发送到超速报警
              log.info("send info overspeed")
              overSpeed ! (deviceId, deviceType, info.id)
            }
            
            if( info.alarmTypeID == AlarmUtils.vehicle_tired){//疲劳驾驶
              log.info("send info triedDrive")
              triedDrive ! (deviceId, deviceType, info.id)
            }
            
            if( info.alarmTypeID == AlarmUtils.area_in_out){//进出区域
              log.info("send info areaInOut")
              areaInOut ! (deviceId, deviceType, info.id)
            }
            
            if( AlarmUtils.alarmSource(info.alarmTypeID) == 20){//电池报警
              log.info("send info batteryAlarm")
              batteryAlarm ! (deviceId, deviceType, info.id)
            }
        })
        }
      }
      
      mileage ! (deviceId,deviceType)//里程统计
    case Stop =>
      log.info("Routing logout!")
      context.system.terminate()
      System.exit(0)
  }

}

